﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Markup;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using MyFloridaHouse.libSvOpenDistricts.Criteria;
using MyFloridaHouse.libSvCommonUtilities;
using MyFloridaHouse.libSvUtilities.Logging;
using MyFloridaHouse.libSvCommonUtilities.Converters;
using System.Windows.Shapes;
using System.Windows.Media;

namespace MyFloridaHouse.libSvOpenDistricts.SummaryDataGrid
{
    public static class DataGridColumnBuilder
    {
        

        public static List<DataGridColumnDescription> BuildDataGridColumnDescriptions(string criteriaDatasetName)
        {
            List<DataGridColumnDescription> newColumnDescriptionList = new List<DataGridColumnDescription>();

            ObservableCollection<CriteriaDatasetDTO> criteriaList = CriteriaDatasetManager.GetCriteriaDataset(criteriaDatasetName);

            newColumnDescriptionList.Clear();
            newColumnDescriptionList.Add(new DataGridColumnDescription("DistrictID", "District ID", "District Number", DataGridColumnDescriptionValueType.intDataType,  null));
            newColumnDescriptionList.Add(new DataGridColumnDescription("PopDev", "Pop Dev", "Population deviation from the ideal", DataGridColumnDescriptionValueType.intDataType, null));


            foreach (var criteria in criteriaList)
            {
                
                DataGridColumnDescriptionValueType criteriaValueType = DataGridColumnDescriptionValueType.intDataType;
                if( (enumCriteriaType.Percent == criteria.criteriaType) | (enumCriteriaType.PercentDifference == criteria.criteriaType) )
                {
                    criteriaValueType = DataGridColumnDescriptionValueType.floatDataType;
                }
                newColumnDescriptionList.Add(new DataGridColumnDescription(criteria.criteriaName, criteria.criteriaHeader, criteria.description, criteriaValueType, criteria));
            }


            return newColumnDescriptionList;
        }

        public static void BuildDataGridColumns(DataGrid dataGrid, List<DataGridColumnDescription> columnDescriptionList)
        {
            // clear out the old columns and re-build them with new criteria
            dataGrid.Columns.Clear();

            // Set up the DataGrid to match the district data summary columns from (defined in the criteria dataset).
            int index = 0;
            foreach (DataGridColumnDescription column in columnDescriptionList)
            {
                if (DataGridColumnDescriptionValueType.floatDataType == column.valueType)
                {
                    dataGrid.Columns.Add(DataGridColumnBuilder.CreateFloatColumn(index++, column.name, column.header, column.description));
                }
                else
                {
                    dataGrid.Columns.Add(DataGridColumnBuilder.CreateIntColumn(index++, column.name, column.header, column.description));
                }
            }
        }

        public static DataGridTextColumn CreateFloatColumn(int index, string fieldName, string header, string description)
        {
            DataGridTextColumn column = new DataGridTextColumn();
            column.Header = header;
            column.HeaderStyle = BuildColumnHeaderStyle(description);
            column.Binding = new Binding("valuesList[" + index + "].floatValue");
            column.Binding.StringFormat = "0.00";
            column.CellStyle = BuildFloatCellStyle(fieldName, description);
            return column;
        }

        public static DataGridTextColumn CreateIntColumn(int index, string fieldName, string header, string description)
        {
            DataGridTextColumn column = new DataGridTextColumn();
            column.Header = header;
            column.HeaderStyle = BuildColumnHeaderStyle(description);
            column.Binding = new Binding("valuesList[" + index + "].intValue");
            column.CellStyle = BuildCellStyle(fieldName, description);
            return column;
        }

        private static Style BuildColumnHeaderStyle(string tooltip)
        {
            FontWeight fw = FontWeights.Bold;
            Style newGridHeaderStyle = new Style(typeof(DataGridColumnHeader));
            newGridHeaderStyle.Setters.Add(new Setter { Property = DataGridColumnHeader.FontSizeProperty, Value = 9.0 });
            newGridHeaderStyle.Setters.Add(new Setter { Property = DataGridColumnHeader.FontWeightProperty, Value = FontWeights.Bold });
            newGridHeaderStyle.Setters.Add(new Setter { Property = DataGridColumnHeader.ContentTemplateProperty, Value = CreateDataGridColumnHeaderTemplate(tooltip) });
            return newGridHeaderStyle;
        }

        private static Style BuildFloatCellStyle(string fieldName, string tooltip)
        {
            Style newGridCellStyle = new Style(typeof(DataGridCell));
            newGridCellStyle.Setters.Add(new Setter { Property = DataGridCell.FontSizeProperty, Value = 11.0 });
            newGridCellStyle.Setters.Add(new Setter { Property = DataGridCell.HorizontalContentAlignmentProperty, Value = HorizontalAlignment.Right });
            return newGridCellStyle;
        }

        private static Style BuildCellStyle(string fieldName, string tooltip)
        {
            Style newGridCellStyle = new Style(typeof(DataGridCell));
            newGridCellStyle.Setters.Add(new Setter { Property = DataGridCell.FontSizeProperty, Value = 11.0 });
            newGridCellStyle.Setters.Add(new Setter { Property = DataGridCell.HorizontalContentAlignmentProperty, Value = HorizontalAlignment.Right });

            //todo: This is how to define a cell template for a datagrid.  Want to be able to control background colors for certain fields, need research
            // some urls describing how to use converters
            //  http://blogs.msdn.com/b/scmorris/archive/2008/04/14/defining-silverlight-datagrid-columns-at-runtime.aspx
            //  http://forums.silverlight.net/forums/p/70078/480429.aspx
            // NOTE: This one is promising, may be able to use different types such as int and double http://stackoverflow.com/questions/59451/creating-a-silverlight-datatemplate-in-code

            //newGridCellStyle.Setters.Add(new Setter { Property = DataGridCell.ContentTemplateProperty, Value = CreateDataGridCellTemplate(fieldName, tooltip) });
            //SolidColorBrush backgroundBrush = new SolidColorBrush(Colors.Cyan);
            //newGridCellStyle.Setters.Add(new Setter { Property = BackgroundProperty, Value = backgroundBrush });

            return newGridCellStyle;
        }


        private static DataTemplate CreateDataGridColumnHeaderTemplate(string tooltip)
        {
            string str = @"<DataTemplate xmlns='http://schemas.microsoft.com/client/2007'>"
                        + @"<ContentControl   Content='{Binding}'>"
                        + @"<ToolTipService.ToolTip>"
                        + @"<ToolTip Content='" + tooltip + "'>"
                        + @"</ToolTip>"
                        + @"</ToolTipService.ToolTip>"
                        + @"</ContentControl >"
                        + @"</DataTemplate>";
            return (DataTemplate)XamlReader.Load(str);
        }

        //private static DataTemplate CreateDataGridCellTemplate(string fieldName, string tooltip)
        //{
        //    string str = @"<DataTemplate xmlns='http://schemas.microsoft.com/client/2007' >"
        //                + @"<ContentControl   Content='{Binding }'>"
        //                + @"</ContentControl >"
        //                + @"</DataTemplate>";
        //    return (DataTemplate)XamlReader.Load(str);
        //}


    }
}
